-
-
Notifications
You must be signed in to change notification settings - Fork 528
feat: Better parsing of external HTML attributes & inline styles #1605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/react
@blocknote/mantine
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-docx-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this looks exactly as I'd have expected it to.
Great work @matthewlipski
e613254
to
1b23c70
Compare
"data-background-color": attributes.stringValue, | ||
}), | ||
}, | ||
stringValue: getBackgroundColorAttribute("stringValue"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this loses default: undefined
, not sure if intended
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really make a difference, there's not really a use case where you would set a color mark with a default value. Selecting a default text/bg color in the color picker removes the mark instead, and this is how it should always be done, so the default value for the mark should never be used.
if (element.hasAttribute("data-text-color")) { | ||
return { stringValue: element.getAttribute("data-text-color") }; | ||
if (element.hasAttribute("data-text-color") || element.style.color) { | ||
return {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return {}?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we don't need to parse the color because it's already handled in the parseHTML
function of the stringValue
attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we omit line 25 / 26 then entirely? it's still a bit confusing right? (maybe tiptap issue)
packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Just realized one caveat; for yjs stored documents the block color data will probably be lost when upgrading (or the document might break entirely, but I doubt that).
Perhaps you can see with Nick if there's a fix for that possible (or a migration path for people upgrading)
<div class="bn-block-column-list" data-node-type="columnList" data-id="1" style="display: flex;"><div class="bn-block-column" data-node-type="column" data-id="2" data-width="1" style="flex-grow: 1;"><p>Column Paragraph 0</p><p>Column Paragraph 1</p></div><div class="bn-block-column" data-node-type="column" data-id="5" data-width="1" style="flex-grow: 1;"><p>Column Paragraph 2</p><p>Column Paragraph 3</p></div></div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any idea why this snapshot is a lot smaller while the other ones got larger?
background-color: #f4dfeb; | ||
} | ||
|
||
/* TEXT ALIGNMENT */ | ||
[data-text-alignment="left"] { | ||
justify-content: flex-start !important; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know why this was here / why has it been removed?
|
||
export const BackgroundColorExtension = Extension.create({ | ||
name: "blockBackgroundColor", | ||
|
||
addGlobalAttributes() { | ||
return [ | ||
{ | ||
types: ["blockContainer", "tableCell", "tableHeader"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose not, but does this break anything for colors / alignment on custom blocks (as you're changing this from blockcontainer to explicitly naming the block types)
|
||
export const TextColorExtension = Extension.create({ | ||
name: "blockTextColor", | ||
|
||
addGlobalAttributes() { | ||
return [ | ||
{ | ||
types: ["blockContainer", "tableCell", "tableHeader"], | ||
types: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question as above; does this break anything for custom blocks?
if (element.hasAttribute("data-text-color")) { | ||
return { stringValue: element.getAttribute("data-text-color") }; | ||
if (element.hasAttribute("data-text-color") || element.style.color) { | ||
return {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we omit line 25 / 26 then entirely? it's still a bit confusing right? (maybe tiptap issue)
This PR adds tests for parsing various HTML attributes & inline styles, and provides fixes so that these tests pass. Namely, these tests include:
start
props fromstart
attribute.width
props fromwidth
attribute.bold
,italic
,underline
, andstrike
marks from various tags & inline styles. This includes e.g.b
tags andfont-weight
inline styles forbold
marks as well asi
tags andfont-style
inline styles foritalic
marks.textColor
andbackgroundColor
marks from inline styles.textColor
andbackgroundColor
props from inline styles.textAlignment
props from inline styles.To ensure all tests pass, several changes have also been made to the code:
textColor,
backgroundColor, and
textAlignment` props for inline styles.textColor
andbackgroundColor
props have been changed, so that they're now applied as attributes toblockContent
nodes instead ofblockContainer
nodes. This is because we would only attempt to parse these props when ablockContainer
node is found, i.e. when adiv
with[data-node-type="blockContainer"]
is found. So we would never even attempt to parse these props from external HTML. The CSS has been updated also to account for this change. Also, all other block props are already stored on theblockContent
node, so this makes thetextColor
andbackgroundColor
props also consistent with that.Additionally, code for color props & marks has been cleaned up slightly.
The large negative diff in this PR is also because a bunch of redundant React unit test snapshots have been removed.